package encrypting;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

/**
 * This class implements a system for encrypting a word.
 * @author Alexandre, Cristovao, Raul, Rodolpho, Tales
 * @version 1.1
 * @milestone Number 2
 */
public class SHA1Encrypt implements Encryptable {

	private static final String ALGORITHM = "SHA-1";
	private static final String ENCODING = "UTF-16";

	/**
	 * Encrypt a word.
	 * @return the encrypted word.
	 */
	public String encryptWord(String word) {
		try {
			MessageDigest message = MessageDigest.getInstance(ALGORITHM);
			message.update(word.getBytes(ENCODING));
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encode(message.digest());
		} catch (NoSuchAlgorithmException e) {
			return word;
		} catch (UnsupportedEncodingException e) {
			return word;
		}
	}
}
